home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / Tickle-4.0 (tcl) / tcl / extend / tests / filecmds.test < prev    next >
Encoding:
Text File  |  1993-10-26  |  4.2 KB  |  153 lines  |  [TEXT/MPS ]

  1. #
  2. # filecmds.test
  3. #
  4. # Tests for the copyfile, and pipe commands.
  5. #---------------------------------------------------------------------------
  6. # Copyright 1992-1993 Karl Lehenbauer and Mark Diekhans.
  7. #
  8. # Permission to use, copy, modify, and distribute this software and its
  9. # documentation for any purpose and without fee is hereby granted, provided
  10. # that the above copyright notice appear in all copies.  Karl Lehenbauer and
  11. # Mark Diekhans make no representations about the suitability of this
  12. # software for any purpose.  It is provided "as is" without express or
  13. # implied warranty.
  14. #------------------------------------------------------------------------------
  15. # $Id: filecmds.test,v 2.4 1993/08/18 05:23:56 markd Exp $
  16. #------------------------------------------------------------------------------
  17. #
  18.  
  19. if {[info procs test] != "test"} then {source testlib.tcl}
  20.  
  21. # Create a test file
  22.  
  23. unlink -nocomplain {IOTEST.TMP IOTEST2.TMP IOTEST3.TMP}
  24.  
  25. set testFH [open IOTEST.TMP w]
  26. set testFileSize 0
  27. for {set cnt 0} {$cnt < 100} {incr cnt} {
  28.      set rec [GenRec $cnt]
  29.      puts $testFH $rec
  30.      incr testFileSize [expr [clength $rec]+1]
  31. }
  32. close $testFH
  33.  
  34. if {$testFileSize != [file size IOTEST.TMP]} {
  35.      error "Wrong file size calculated for IOTEST.TMP"
  36. }
  37.  
  38. Test filecmds-3.1 {copyfile tests} {
  39.     set testFH [open IOTEST.TMP r]
  40.     set testFH2 [open IOTEST2.TMP w]
  41.     copyfile $testFH $testFH2
  42.     close $testFH
  43.     close $testFH2
  44.     system "diff IOTEST.TMP IOTEST2.TMP >/dev/null 2>&1"
  45. } 0 0
  46.  
  47. Test filecmds-3.2 {copyfile tests} {
  48.     set testFH [open IOTEST3.TMP w]
  49.     set testFH2 [open IOTEST2.TMP w]
  50.     set stat [list [catch {copyfile $testFH $testFH2} msg] \
  51.                    [lrange $msg 1 end]]
  52.     close $testFH
  53.     close $testFH2
  54.     set stat
  55. } 0 {1 {wasn't opened for reading}}
  56.  
  57. Test filecmds-3.3 {copyfile tests} {
  58.     set testFH [open IOTEST.TMP r]
  59.     set testFH2 [open IOTEST2.TMP r]
  60.     set stat [list [catch {copyfile $testFH $testFH2} msg] \
  61.                    [lrange $msg 1 end]]
  62.     close $testFH
  63.     close $testFH2
  64.     set stat
  65. } 0 {1 {wasn't opened for writing}}
  66.  
  67. Test filecmds-3.4 {copyfile tests} {
  68.     copyfile $testFH $testFH2
  69. } 1 "file \"$testFH\" isn't open"
  70.  
  71. Test filecmds-3.5 {copyfile tests} {
  72.     copyfile
  73. } 1 {wrong # args: copyfile ?-bytes num|-maxbytes num? fromFileId toFileId}
  74.  
  75. foreach flag {-bytes -maxbytes} {
  76.     Test filecmds-3.6.$flag {copyfile tests} {
  77.         set copySize [expr ($testFileSize*2)/3]
  78.         set testFH [open IOTEST.TMP r]
  79.         set testFH2 [open IOTEST2.TMP w]
  80.         copyfile $flag $copySize $testFH $testFH2
  81.         close $testFH
  82.         close $testFH2
  83.  
  84.         set testFH [open IOTEST.TMP r]
  85.         set testData [read $testFH $copySize]
  86.         close $testFH
  87.  
  88.         set testFH2 [open IOTEST2.TMP r]
  89.         set testData2 [read $testFH2]
  90.         close $testFH2
  91.  
  92.         list [expr [file size IOTEST2.TMP] == $copySize] \
  93.              [string compare $testData $testData2]
  94.     } 0 {1 0}
  95.  
  96.     catch {unset testData testData2}
  97. }
  98.  
  99. set copySize [expr $testFileSize*2]
  100.  
  101. Test filecmds-3.7 {copyfile tests} {
  102.     set testFH [open IOTEST.TMP r]
  103.     set testFH2 [open IOTEST2.TMP w]
  104.     set stat [catch {copyfile -bytes $copySize $testFH $testFH2} msg]
  105.     close $testFH
  106.     close $testFH2
  107.     list $stat $msg
  108. } 0 [list 1 \
  109.           "premature EOF, $copySize bytes expected, $testFileSize bytes actually read"]
  110.  
  111. Test filecmds-3.7 {copyfile tests} {
  112.     set testFH [open IOTEST.TMP r]
  113.     set testFH2 [open IOTEST2.TMP w]
  114.     set stat [catch {copyfile -maxbytes $copySize $testFH $testFH2} msg]
  115.     close $testFH
  116.     close $testFH2
  117.     list $stat $msg
  118. } 0 [list 0 $testFileSize]
  119.  
  120. pipe readPF writePF
  121.  
  122. flush stdout  ;# Not going to exec, must clean up the buffers.
  123. flush stderr
  124. set sonPid [fork]
  125.  
  126. if {$sonPid == 0} {
  127.     for {set cnt 0} {$cnt < 50} {incr cnt} {
  128.         Test filecmds-4.1 {pipe tests} {
  129.             if {![gets $readPF msgBuf]} {
  130.                set msgBuf "Premature eof on pipe"
  131.             }
  132.             set msgBuf
  133.         } 0 [GenRec $cnt]
  134.     }
  135.     close $readPF
  136.     exit 0
  137. }
  138.  
  139. for {set cnt 0} {$cnt < 50} {incr cnt} {
  140.     puts $writePF [GenRec $cnt]
  141. }
  142. flush $writePF
  143. Test filecmds-4.2 {pipe tests} {
  144.     wait $sonPid
  145. } 0 "$sonPid EXIT 0"
  146.  
  147. close $readPF
  148. close $writePF
  149.  
  150. unlink -nocomplain {IOTEST.TMP IOTEST2.TMP IOTEST3.TMP}
  151.  
  152.  
  153.